package com.zsy.controller;

import com.zsy.dto.DishDto;
import com.zsy.pojo.Dish;
import com.zsy.pojo.Page;
import com.zsy.pojo.R;
import com.zsy.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;


    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session) {
        Long usernameId = (Long) session.getAttribute("result");

        dishDto.setUpdateUser(usernameId);
        dishDto.setCreateUser(usernameId);

        dishService.save(dishDto);

        return R.success("保存成功");

    }

    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize, String name){

       return R.success(dishService.page(page,pageSize,name));

    }
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable("id") Long id){

        DishDto dishDto = dishService.findById(id);

        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
        long result = (long) session.getAttribute("result");

        dishDto.setUpdateUser(result);

        dishService.update(dishDto);

        return R.success("保存成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId, Integer status){

        List<DishDto> list = dishService.dishList(categoryId ,status);

        return R.success(list);
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus( @PathVariable("status") Integer status , @RequestParam List<Long> ids){
        dishService.updateStatus(status,ids);

        return R.success("修改成功");

    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        dishService.delete(ids);
        return R.success("删除成功");

    }

}
